package com.frcdi.qms.system.service.impl;

import com.frcdi.qms.common.config.EmailConfig;
import com.frcdi.qms.common.config.QmsConfig;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.system.domain.QmsMessage;
import com.frcdi.qms.system.mapper.QmsMessageMapper;
import com.frcdi.qms.system.service.IEmailService;
import com.frcdi.qms.system.service.IQmsMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * @ClassName IEmailServiceImpl
 * @Author Mr.Han
 * @create 2023/8/3 13:17
 */
@Service
public class IEmailServiceImpl implements IEmailService {

    @Autowired
    private EmailConfig emailConfig;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private QmsMessageMapper messageMapper;

    /**
     * @Param [sendTo, title, content]
     * @return void
     * @Author Mr.Han
     * @Description //TODO 发送简单邮件
     * @throw
     **/
    @Override
    public void sendMailSimpleMail(String sendTo, String title, String content) {


        //简单邮件的发送
        SimpleMailMessage message= new SimpleMailMessage();
        message.setFrom(emailConfig.getSendName());
        message.setTo(sendTo);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);

    }

    /**
     * @Param [sendTo, title, content]
     * @return void
     * @Author Mr.Han
     * @Description //TODO  发送一个带html格式的邮件
     * @throw
     **/
    @Override
    public void sendHtmlMail(String sendTo, String title, String content) {

        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(emailConfig.getSendName());
            mimeMessageHelper.setTo(sendTo);
            mimeMessageHelper.setSubject(title);
            mimeMessageHelper.setText(content, true);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Param [sendTo, title, content]
     * @return void
     * @Author Mr.Han
     * @Description //TODO 发送带附件格式的邮件
     * @throw
     **/
    @Transactional
    @Override
    public void sendAttachmentMail(String sendTo, String title, String content, List<com.frcdi.qms.masterdata.domain.File> fileList) {
        MimeMessage mimeMailMessage = null;
        /*QmsMessage qmsMessage = new QmsMessage();
        qmsMessage.setContent(content);
        qmsMessage.setRemark("email_remark");
        messageMapper.insertQmsMessage(qmsMessage);
        List<QmsMessage> qmsMessages = messageMapper.selectQmsMessageList(qmsMessage);*/
        try {
            mimeMailMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true, "UTF-8");
            mimeMessageHelper.setFrom(emailConfig.getSendName());
            mimeMessageHelper.setTo(sendTo);
            mimeMessageHelper.setSubject(title);
            mimeMessageHelper.setText(content,true);
            if (fileList != null && !fileList.isEmpty()) {
                // 这里放置当fileList不为空且至少有一个元素时执行的代码
                for (com.frcdi.qms.masterdata.domain.File sampleFile : fileList) {
                    //文件路径
                    FileSystemResource file = new FileSystemResource(new File(QmsConfig.getProfile() + sampleFile.getFilePath().split("profile")[1]));
                    mimeMessageHelper.addAttachment(sampleFile.getFileName(), file);
                }
            }


            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
